package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.live.LiveSessionService;
import com.treasure.mall.biz.service.core.sms.SMSService;
import com.treasure.mall.biz.service.core.user.UserService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbq.common.ienum.sms.SmsTemplateSceneType;
import com.treasure.mall.zbqmallapi.annotation.AccessControl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.controller
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/7/11 10:07
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.zbqmallapi.controller Inc. All rights reserved.
 */
@RestController
@RequestMapping("/sms")
@AccessControl
@Api(tags = "短信模块")
public class SmsController {

    @Autowired
    private SMSService smsService;
    @Autowired
    private LiveSessionService liveSessionService;
    @Autowired
    private UserService userService;

    @ApiOperation("发送短信验证码")
    @PostMapping("/send")
    public ApiResult send(String mobile) {
        UserPO userPO = userService.findByUsername(mobile);
        if (userPO != null && liveSessionService.checkPlaying(userPO.getUserId())) {
            return ApiResult.error(ResultCodeEnum.ALERT_FAILED, "当前用户目前正在直播,无法登录");
        }
        if (smsService.sendSmsVerifyCode(mobile, SmsTemplateSceneType.SMS_SCENE_IDENTITY)) {
            return ApiResult.ok();
        }
        return ApiResult.error(ResultCodeEnum.SMS_FAILED);
    }

}
